<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <style>
    </style>
</head>
<style>
    .home:before {
        content: url(../image/home.png);
    }

    .refresh:before {
        content: url(../image/refresh.png);
    }

    .company:before {
        content: url(../image/company.png);
    }

    .pa-title {
        font-size: 17px;
        color: black;
        font-weight: bold;
    }

    .pa-title-content {
        font-size: 17px;
        color: black;
    }

    body {
        background-color: #D9D8DB;
    }


    .pa_xg_t {
        background-color: #254060;
        height: 240px;
        color: white;
        overflow: auto;
    }

    .pa_xg {
        margin-top: 5px;
        height: 40px;
        line-height: 40px;
        margin-left: 2%;
        text-align: center;
        color: white;
    }

    .pa_xg input {
        height: 40px;
        min-height: 40px;
    }

    .pa_xg_but {
        height: 50px;
        line-height: 50px;
        margin-left: 70px;
        float: left;
        width: 130px;
        font-size: 27px;
        color: white;
        text-align: center;
    }

    p {
        font-size: 17px;
        color: black;
        font-weight: bold;
        text-align: center;
    }

    table {
        width: 100%;
        table-layout: fixed;
        word-wrap: break-word;
    }

    td {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }

    td input {
        text-align: center;
    }
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #D9D8DB">
        <div style="width: 10%;height: 60px;float: left;padding-top: 6px;">
            <a class="aui-pull-left aui-btn ">
                <span class="aui-iconfont company"></span>
            </a>
        </div>
        <div style="width: 10%;height: 60px;float: left;">
            <div style="text-align: left;">
                <span class="pa-title">富晟安创</span>
            </div>
        </div>
        <div style="width: 70%;height: 60px;float: left;">
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="locationName"></span>
            </div>
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCenterName"></span>
            </div>
            <div style="width:24%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCellName"></span>
            </div>
            <div style="width:40%; float: left; text-align: left;">
                <span class="pa-title-content" id="nowDate"></span>
            </div>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn" onclick="closeWin()">
                <span class="aui-iconfont home"></span>
            </a>
        </div>
    </header>
    <div class="aui-content">
        <div class="aui-flex-col aui-flex-center" style="height: 30px;">
            <div class="aui-flex-item-12">
                <p>下挂</p>
            </div>
        </div>

        <div class="aui-flex-col aui-flex-center">
            <div class="aui-flex-item-12">
                <div class="pa_xg_but" style="background-color: #00BFBF;" onclick="doSearch(0)">
                    查询
                </div>

                <div class="pa_xg_but" style="background-color: #F59A23;" onclick="openDefect(2)">
                    缺陷录入
                </div>

                <div class="pa_xg_but" style="background-color: #528302;" onclick="save(1)">
                    提交
                </div>

                <div class="pa_xg_but" style="background-color: #D9001B;" onclick="cancle()">
                    关闭
                </div>
            </div>
        </div>
        <div class="aui-flex-col aui-flex-center" style="margin-top: 1%;background-color: #254060;">
            <div class="aui-flex-item-12 pa_xg">
                <div class="aui-list-item-inner" style="border: 0;">
                    <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                        飞&nbsp&nbsp巴&nbsp&nbsp号：</div>
                    <div class="aui-list-item-input" style="background-color: white;float: left;width: 250px;">
                        <input type="text" id="fbCode" class="aui-input" onclick="getIdSelector();" />
                    </div>
                </div>
            </div>

            <div class="aui-flex-item-12 pa_xg">
                <div class="aui-flex-item-5">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                            <span>工&nbsp&nbsp单&nbsp&nbsp号：</span>
                            <span id="workOrderCode"></span>
                        </div>
                    </div>
                </div>

                <div class="aui-flex-item-5">
                    <div class="aui-flex-col">
                        <div class="aui-list-item-inner" style="border: 0;">
                            <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                <span>上挂数量：</span>
                                <span id="hangUpQty"></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="aui-flex-item-12 pa_xg">
                <div class="aui-flex-item-5">
                    <div class="aui-list-item-inner" style="border: 0;">
                        <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                            <span>物料编码：</span>
                            <span id="mrlCode"></span>
                        </div>
                    </div>
                </div>

                <div class="aui-flex-item-5">
                    <div class="aui-flex-col">
                        <div class="aui-list-item-inner" style="border: 0;">
                            <div class="aui-list-item-label" style="font-size: 20px;float: left;">
                                <span>物料名称：</span>
                                <span id="mrlName"></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="height: 10px;background-color: #254060;"></div>
        <div class="aui-flex-col aui-flex-center">
            <div class="aui-flex-item-12">
                <div class="aui-flex-col pa_xg_t">
                    <div class="aui-flex-item-12">
                        <div style="height: 100%;width: 100%;">
                            <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                                <thead>
                                    <tr style="text-align: center;color: white;font-size: 18px;">
                                        <td algin='center' style="width:8%">序号</td>
                                        <td algin='center' style="width:16%">上挂容器码</td>
                                        <td algin='center' style="width:18%">上挂物料号</td>
                                        <td algin='center' style="width:18%">上挂物料名</td>
                                        <td algin='center' style="width:10%">批次号</td>
                                        <td algin='center' style="width:10%">上挂数</td>
                                        <td algin='center' style="width:10%">下挂数</td>
                                        <td algin='center' style="width:10%">不合格数</td>
                                        <td algin='center' style="display:none;">打捞数</td>
                                        <td algin='center' style="display: none;">sysid</td>
                                    </tr>
                                </thead>
                                <tbody id="hangUpRecordDataHtml">
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";
    var list = [];
    var josnList;
    var refloatFlag;
    var workOrderCode;
    var unQualifyQtySum = 0;
    var submitBut = false;

    //光标所在id
    var idSelector = "";

    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //产线工位信息
        document.getElementById("locationName").innerHTML = api.pageParam.locationName;
        document.getElementById("workCenterName").innerHTML = api.pageParam.workCenterName;
        document.getElementById("workCellName").innerHTML = api.pageParam.workCellName;
        document.getElementById("nowDate").innerHTML = getTimeCommon();
        //当前时间刷新
        window.setInterval("getTime()", 1000);

        api.setFocus({
            inputId: 'fbCode',
            focus: true
        });
        submitBut = false;
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //时间刷新
    function getTime() {
        document.getElementById("nowDate").innerHTML = getTimeCommon();
    }

    //查询上挂记录
    function doSearch(flag) {
        var fbCode = $("#fbCode").val();
        if (fbCode == null || fbCode == "" || fbCode == undefined) {
            api.toast({
                msg: "请录入飞巴号!",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyHangUpData";
        var data = {
            values: {
                fbCode: fbCode,
                state: 0,  //状态：0-上挂，1-下挂
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                submitBut = false;
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    list = eval(myobj.list);
                    var html = "";


                    if (list != null && list.length > 0) {
                        document.getElementById("workOrderCode").innerHTML = list[0].workOrderCode;
                        document.getElementById("hangUpQty").innerHTML = myobj.hangUpQty;
                        document.getElementById("mrlCode").innerHTML = list[0].ext01;
                        document.getElementById("mrlName").innerHTML = list[0].ext02;

                        var html = "";
                        //遍历
                        for (var i = 0; i < list.length; i++) {
                            var hangUpQtyId = "hangUpQty_" + i;
                            var hangDownQtyId = "hangDownQty_" + i;
                            var unQualifyQtyId = "unQualifyQty_" + i;
                            var refloatQtyId = "refloatQty_" + i;
                            html += "<tr style='text-align: center;color: white;font-size: 14px;height:30px;'>";
                            html += "<td name='index'>" + (i + 1) + "</td>";
                            html += "<td name='ctBarCode'>" + list[i].ctBarCode + "</td>";
                            html += "<td name='sitmCode'>" + list[i].sitmCode + "</td>";
                            html += "<td name='sitmName'>" + list[i].sitmName + "</td>";
                            html += "<td name='lotCode'>" + list[i].lotCode + "</td>";
                            html += "<td name='hangUpQty'>" + list[i].hangUpQty + "</td>";
                            html += "<td><input type='number' id=\"" + hangDownQtyId + "\" style='color:white; font-size: 14px; height:30px; min-height:30px;' value=\"" + list[i].hangUpQty + "\" /></td>";
                            html += "<td><input type='number' id=\"" + unQualifyQtyId + "\" style='color:white; font-size: 14px; height:30px; min-height:30px;' value=\"" + list[i].unQualifyQty + "\" /></td>";
                            html += "<td style='display:none;'><input type='number' id=\"" + refloatQtyId + "\" style='color:white; font-size: 14px; height:30px; min-height:30px;' value=\"" + list[i].refloatQty + "\" /></td>";
                            html += "<td name='sysid' style='display:none;'>" + list[i].sysid + "</td>";
                            html += "</tr>";
                        }

                        $api.html($api.byId("hangUpRecordDataHtml"), html);
                    } else {

                        //清空表单
                        clearForm();
                        if (flag == 0) {
                            api.toast({
                                msg: "飞巴号【" + fbCode + "】没有上挂记录!",
                                duration: 3000
                            });
                        }
                    }

                } else {
                    //清空表单
                    clearForm();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!");
            }
        });
    }

    //缺陷录入
    async function openDefect(flag) {
        if (submitBut) {
            if (unQualifyQtySum == 0) {
                api.toast({
                    msg: "不合格数为【" + unQualifyQtySum + "】",
                    duration: 3000
                });
                return;
            }

            //未提交
        } else {
            //先保存
            await saveChk(flag);
            var msg = await submit(flag);

            //保存成功，不合格数大于0，打开缺陷录入界面
            if (msg == "S") {
                if (unQualifyQtySum == 0) {
                    api.toast({
                        msg: "不合格数为【" + unQualifyQtySum + "】",
                        duration: 3000
                    });
                    return;
                }
            } else {
                return;
            }
        }
        var fbCode = $("#fbCode").val();
        var pageParam = api.pageParam;
        pageParam.name = "productionAssemblyHangDown";
        pageParam.fbCode = fbCode;
        pageParam.workOrderCode = workOrderCode;
        api.openWin({
            name: "productionAssemblyHangDown_unQty",
            url: 'productionAssemblyHangDown_unQty.html',
            pageParam: pageParam,
        });
    }

    //清空表单
    function clearForm() {
        document.getElementById("workOrderCode").innerHTML = "";
        document.getElementById("hangUpQty").innerHTML = "";
        document.getElementById("mrlCode").innerHTML = "";
        document.getElementById("mrlName").innerHTML = "";
        $api.html($api.byId("hangUpRecordDataHtml"), "");
    }

    async function save(flag) {
        await saveChk(flag);
        await submit(flag);
    }

    //保存
    function saveChk(flag) {
        return new Promise(resolve => {

            //表格数据为空
            if (list == null || list.length == 0) {
                api.toast({
                    msg: "没有需要保存的数据!",
                    duration: 3000
                });
                return;
            }

            //上挂数量
            var hangUpQty = 0;
            //下挂数量
            var hangDownQty = 0;
            //不合格数量 
            var unQualifyQty = 0;
            //打捞数量
            var refloatQty = 0;
            //合格数
            var qualifyQty = 0;
            var sumRefloatQty = 0;
            josnList = [];
            for (var i = 0; i < list.length; i++) {
                hangUpQty = list[i].hangUpQty;
                hangDownQty = document.getElementById("hangDownQty_" + i).value;
                unQualifyQty = document.getElementById("unQualifyQty_" + i).value;
                // refloatQty = document.getElementById("refloatQty_" + i).value;

                //下挂数量判空
                if (hangDownQty == null || hangDownQty == "" || hangDownQty == undefined) {
                    api.toast({
                        msg: "第【" + (i + 1) + "】行下挂数量不能为空!",
                        duration: 3000
                    });
                    return;
                }

                //不合格数量判空
                if (unQualifyQty == null || unQualifyQty == "" || unQualifyQty == undefined) {
                    api.toast({
                        msg: "第【" + (i + 1) + "】行不合格数量不能为空!",
                        duration: 3000
                    });
                    return;
                }

                // //打捞数量判空
                // if (refloatQty == null || refloatQty == "" || refloatQty == undefined) {
                //     api.toast({
                //         msg: "第【" + (i + 1) + "】行打捞数量不能为空!",
                //         duration: 3000
                //     });
                //     return;
                // }

                //下挂数量不能大于上挂数量
                if (parseInt(hangDownQty) > parseInt(hangUpQty)) {
                    api.toast({
                        msg: "第【" + (i + 1) + "】行下挂数量【" + hangDownQty + "】不能大于上挂数量【" + hangUpQty + "】!",
                        duration: 3000
                    });
                    return;
                }

                // //打捞数量不能大于上挂数量
                // if (parseInt(refloatQty) > parseInt(hangUpQty)) {
                //     api.toast({
                //         msg: "第【" + (i + 1) + "】行打捞数量【" + refloatQty + "】不能大于上挂数量【" + hangUpQty + "】!",
                //         duration: 3000
                //     });
                //     return;
                // }

                //不合格数不能大于下挂数量
                if (parseInt(unQualifyQty) > parseInt(hangDownQty)) {
                    api.toast({
                        msg: "第【" + (i + 1) + "】行不合格数量【" + unQualifyQty + "】不能大于下挂数量【" + hangDownQty + "】!",
                        duration: 3000
                    });
                    return;
                }

                // //下挂数量+打捞数量等于上挂数量
                // if (parseInt(hangDownQty) + parseInt(refloatQty) != parseInt(hangUpQty)) {
                //     api.toast({
                //         msg: "第【" + (i + 1) + "】行下挂数量【" + hangDownQty + "】+打捞数量【" + refloatQty + "】应等于上挂数量【" + hangUpQty + "】!",
                //         duration: 3000
                //     });
                //     return;
                // }

                //合格数=下挂数量-不合格数量
                qualifyQty = parseInt(hangDownQty) - parseInt(unQualifyQty);
                //打捞数量
                refloatQty = parseInt(hangUpQty) - parseInt(hangDownQty);
                //打捞数量汇总
                sumRefloatQty += parseInt(refloatQty);
                //不合格数汇总
                unQualifyQtySum += parseInt(unQualifyQty);
                josnList.push({
                    "sysid": list[i].sysid,
                    "hangDownQty": hangDownQty,
                    "unQualifyQty": unQualifyQty,
                    "qualifyQty": qualifyQty,
                    "refloatQtyQty": refloatQty
                });
            }

            //打捞数量大于0，弹框提示
            if (sumRefloatQty > 0) {

                var dialogBox = api.require('dialogBox');
                dialogBox.alert({
                    texts: {
                        title: '',
                        content: '下挂数量不等于上挂数量，是否生成打捞记录？',
                        leftBtnTitle: '否',
                        rightBtnTitle: '是'
                    },
                    styles: {
                        bg: '#fff',
                        w: 300,
                        corner: 2,
                        title: {
                            marginT: 20,
                            // icon: 'widget://res/gou.png',
                            iconSize: 40,
                            titleSize: 14,
                            titleColor: '#000'
                        },
                        content: {
                            color: '#000',
                            size: 14
                        },
                        left: {
                            marginB: 7,
                            marginL: 20,
                            w: 130,
                            h: 35,
                            corner: 2,
                            bg: 'rgba(0,0,0,0)',
                            size: 12
                        },
                        right: {
                            marginB: 7,
                            marginL: 10,
                            w: 130,
                            h: 35,
                            corner: 2,
                            bg: 'rgba(0,0,0,0)',
                            size: 12
                        }
                    }
                }, function (ret) {
                    //否
                    if (ret.eventType == 'left') {
                        var dialogBox = api.require('dialogBox');
                        dialogBox.close({
                            dialogName: 'alert'
                        });

                        refloatFlag = 0;

                        resolve("S");

                        //是
                    } else {
                        var dialogBox = api.require('dialogBox');
                        dialogBox.close({
                            dialogName: 'alert'
                        });

                        refloatFlag = 1;
                        resolve("S");
                    }
                });

            } else {
                refloatFlag = 0;
                resolve("S");
            }
        });
    }

    //保存提交
    function submit(flag) {
        return new Promise(resolve => {
            var un = localStorage.getItem('un');
            var serverUrl = localStorage.getItem('serverUrl');
            var url = serverUrl + "/app/getAssemblyHangDownSave";
            var data = {
                values: {
                    josnList: JSON.stringify(josnList),
                    proddate: api.pageParam.proddate,
                    workCenterCode: api.pageParam.workCenterCode,
                    workCenterName: api.pageParam.workCenterName,
                    shiftCode: api.pageParam.shiftCode,
                    shiftName: api.pageParam.shiftName,
                    teamCode: api.pageParam.teamCode,
                    teamName: api.pageParam.teamName,
                    refloatFlag: refloatFlag,//是否生成打捞记录标识：0-不生成，1-生成
                    un: un,
                }
            };

            var params = new Object();
            params.url = url;
            params.dataType = 'json';
            params.method = 'post';

            api.showProgress({
                title: '保存中...',
            });

            ajaxForData(params, data, false, function (ret, err) {
                if (ret) {
                    api.hideProgress();
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");

                    //成功
                    if (myobj.code == 0) {

                        //保存按钮点击
                        if (flag == 1) {
                            api.toast({
                                msg: "保存成功!",
                                duration: 3000
                            });
                        }
                        workOrderCode = document.getElementById("workOrderCode").innerHTML;
                        // doSearch(flag);
                        submitBut = true;
                        resolve("S");
                    } else {
                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });

                        resolve("E");
                    }
                } else {
                    api.hideProgress();
                    alert("网络连接超时!");
                    resolve("E");
                }
            });
        });
    }

    $("#fbCode").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            // console.log(tm);
            if (tm != null && tm != "" && tm != undefined) {
                $("#fbCode").blur();

                //查询下挂数据
                doSearch(1);
            }
        }
    });

    //取消按钮
    function cancle() {
        api.closeWin({});
        api.execScript({
            name: api.pageParam.name,
            script: 'refresh()'
        });
    }

    function refresh() {
        doSearch(1);
    }

    //返回按钮
    function closeWin() {
        api.closeToWin({ name: api.pageParam.rootName });
        api.execScript({
            name: api.pageParam.rootName,
            script: 'refresh()'
        });
    }
</script>

</html>